package com.nhjf.dao.student;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.student.Student;
import com.nhjf.pojo.qo.studnet.StudentQo;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class StudentDao extends BaseDao<Student,StudentQo> {
    @Override
    protected Criteria buildCriteria(Criteria criteria, StudentQo qo) {
        if(qo!=null){
        	criteria.setFetchMode("user", qo.isSeachStudent()?FetchMode.JOIN:FetchMode.SELECT);	
            if(qo.getBirthday() != null){
                criteria.add(Restrictions.eq("birthday", qo.getBirthday()));
            }
            if(qo.getUserid()!=null) {
            	 criteria.add(Restrictions.eq("user.id",qo.getUserid()));
            }
            if(StringUtils.isNotBlank(qo.getName())) {
            	 criteria.add(Restrictions.like("name", qo.getName(),MatchMode.ANYWHERE));
            }
            if(qo.getPhone()!=null) {
           	 criteria.add(Restrictions.like("phonenumber", qo.getPhone(),MatchMode.ANYWHERE));
           }
            if(StringUtils.isNotBlank(qo.getNickName())) {
            	criteria.add(Restrictions.eq("nickname", qo.getNickName()));
            }
            if(qo.getStudentId()!=null) {
            	criteria.add(Restrictions.eq("id", qo.getStudentId()));
            }
            criteria.addOrder(Order.asc("registrationtime"));
        }
        return criteria;
    }

    @Override
    protected Class<Student> getEntityClass() {
        return Student.class;
    }
}
